import java.util.Stack;

/**
 * @author LKQ
 * @date 2022/3/2 17:29
 * @description 单调栈
 */
public class FormalSolution {
    Stack<Integer> prices, weights;

    public FormalSolution() {
        prices = new Stack();
        weights = new Stack();
    }

    public int next(int price) {
        int w = 1;
        while (!prices.isEmpty() && prices.peek() <= price) {
            // 若栈不为空，且栈顶元素小于当前价格，将栈顶元素弹出，然后加入price，实现单调栈
            prices.pop();
            // 弹出元素后，对应的连续日数也要弹出并更新
            w += weights.pop();
        }

        prices.push(price);
        weights.push(w);
        return w;
    }
}
